
/**
 * 自定义返回值
 * return ResultData.ok({ data: '你好', msg: '你好' })
 */


export type ResultDataType = {
  code?: number
  msg?: string
  data?: any
}

export enum CODE {
  // 成功
  SUCCESS = 200,

  // 用户的错误
  WARNING = 203,
}


/**
 * 返回值限定
 */
export class ResultData {
  resultData: Required<ResultDataType>

  constructor(resultData: Required<ResultDataType>) {
    this.resultData = resultData
  }


  static ok(result: Omit<ResultDataType, 'code'>): ResultData {
    return new ResultData({ code: CODE.SUCCESS, msg: result.msg || 'ok', data: result.data || {} })
  }


  static fail(result: Omit<ResultDataType, 'code'>): ResultData {
    return new ResultData({ code: CODE.WARNING, msg: result.msg || 'fail', data: result.data || {} })
  }
}

